package cn.tedu.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

/*
---------------数据统计输出到hadoop---------------
 */
public class WordCountMain {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        //构建job作业对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, WordCountMain.class.getSimpleName());
        //如果上传到服务器运行,加上一句话
        job.setJarByClass(WordCountMain.class);

        //第一步,加载数据,指定TextInputFormat
        job.setInputFormatClass(TextInputFormat.class);
        TextInputFormat.addInputPath(job,
                new Path("hdfs://hadoop01:8020/test/input/wordcount.txt"));

        //第二步,指定mapper
        job.setMapperClass(WordMapper.class);
        //指定数据输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        //第七步,zhidingreducer
        job.setReducerClass(WordReducer.class);
        //指定reducer端输出数据类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        //第八步,指定TextOutputFormat,数据输出
        job.setOutputFormatClass(TextOutputFormat.class);
        TextOutputFormat.setOutputPath(job,new Path("hdfs://hadoop01:8020/text/output"));

        //提交任务等待完成
        job.waitForCompletion(true);
    }
}
